/*
Same as basic QALY analysis, but adding back in user health controls
*/


/*
Repeat 4-analyze_user_choices.do
BUT with additional information created in 5a-create_additional_comparison_pair_characteristics.do
*/

clear
use experimental_user_panel

// 3) set up the same regressions as before (inertia on average, IV)
// basic_reduced_form_analysis.do

// generate variables: initial_choice, chose_entry_drug (in a given quarter), rel_quart
// a few repeats, need to deal with this in a good way
unique enrolid comparison_id quarter

rename entering_drug initial_choice // whether the person's initial choice was the entering drug for a given comparison

drop rel_quarter
gen rel_quart = quarter - entry_quarter
gen chose_entry_drug = (matched_name_id == drug_id)


// 4) show graphs
// 5) Do heterogeneity by QALY difference, priority review
merge m:1 comparison_id using "chambers_pairs", keepus(incremental_qaly max_incremental_qaly priority)
keep if _merge == 1 | _merge == 3 // some will be missing
drop _merge

merge m:1 comparison_id using "comparison_level_similarity_variables"
keep if _merge == 3
drop _merge


// Filter out non-chronic drugs
merge m:1 matched_name_id using "median_prescriptions_stats_drug_level"
keep if _merge == 3
drop _merge
// 2 is the threshold in Alpert (JHE)
keep if median_prescriptions >= 2
// END filter


// summary stats
summ incremental_qaly, detail // AVG: 0.04, SD: 0.14
unique matched_name_id
unique comparison_id

foreach v in incremental_qaly max_incremental_qaly priority same_form same_form_strength {
	
	gen initial_`v' = initial_choice * `v'
	gen treat_`v' = treat * `v'
}



/*
Add in the health controls again (from 4c)
*/


// 1) merge on demographics

merge m:1 enrolid using "all_enrolid_info" // age, sex, emprel, plantyp
keep if _merge == 3
drop _merge

gen age = year - dobyr
gen female = (sex == "2")


//qaly_user_inpatient, qaly_user_outpatient, qaly_user_drugs
merge m:1 enrolid quarter using qaly_user_inpatient // inpatient_events, inpatient_spending, days
keep if _merge == 1 | _merge == 3
drop _merge 

merge m:1 enrolid quarter using qaly_user_outpatient // outpatient_spending
keep if _merge == 1 | _merge == 3
drop _merge 

merge m:1 enrolid quarter using qaly_user_drugs // distinct_drugs, prescription_spending
keep if _merge == 1 | _merge == 3
drop _merge 

foreach v in inpatient_events inpatient_spending days outpatient_spending distinct_drugs prescription_spending {
	
	replace `v' = 0 if `v' == .
}


// 3) take a snapshot of health characteristics at the start of their current regimen
sort enrolid comparison_id quarter
egen t = tag(enrolid comparison_id)
tab first_use if t==1


// 4) Construct variables for analysis
rename entering_drug initial_choice 
gen inpatient = (inpatient_events > 0)
gen tot_spending = outpatient_spending + inpatient_spending + prescription_spending

foreach v in inpatient tot_spending distinct_drugs female age {
	
	gen initial_`v' = `v' if t == 1
	replace initial_`v' = initial_`v'[_n-1] if enrolid == enrolid[_n-1] & comparison_id == comparison_id[_n-1]
	
	gen treat_`v' = treat * initial_`v'
	gen choice_`v' = initial_choice * initial_`v'
}



forvalues i=3(3)15 {
	
	display "Offset: `i' quarters"	
	eststo: ivregress 2sls chose_entry_drug (initial_choice initial_incremental_qaly initial_same_form choice_female choice_age choice_inpatient choice_distinct_drugs =treat treat_incremental_qaly treat_same_form treat_female treat_age treat_inpatient treat_distinct_drugs ) if rel_quart == `i', vce(cluster comparison_id) 	
}


esttab using iv_same_drug_chambers_inc_qaly_w_health_form_cl.csv, con r2 se label replace keep(initial_choice initial_same_form choice_incremental_qaly choice_female choice_age choice_inpatient choice_distinct_drugs)
eststo clear

